﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;
using Vit.Common.Helpers;

namespace Vit.Web.Mvc.Attributes
{
    [AttributeUsage(AttributeTargets.Method)]
    public class WidgetAttribute : Attribute
    {
        private string[] inputParameters = null;
        private Dictionary<string, string> parameters;

        public WidgetAttribute()
        {
        }

        public WidgetAttribute(params string[] parameters)
        {
            this.inputParameters = parameters;
        }

        private void SetWidgetParameters()
        {
            // First execution sets and prevents re-entrancy
            if (this.parameters == null)
            {
                // Set empty dictionary in place till we do our work
                this.parameters = new Dictionary<string, string>();
                Dictionary<string, string> controlParameters = new Dictionary<string, string>();

                string[] inputControlParameters = this.inputParameters;

                // GC the ctor argument -- no longer needed
                this.inputParameters = null;

                if (inputControlParameters == null || inputControlParameters.Length == 0)
                {
                    return;
                }

                if (inputControlParameters.Length % 2 != 0)
                {
                    throw new InvalidOperationException(Locale.GetString("Vit.Web", "NeedEvenNumberOfControlParameters"));
                }

                for (int i = 0; i < inputControlParameters.Length; i += 2)
                {
                    string key = inputControlParameters[i];
                    string value = inputControlParameters[i + 1];
                    if (key == null)
                    {
                        throw new InvalidOperationException(string.Format(Locale.GetString("Vit.Web", "ControlParameterKeyIsNull"), i));
                    }
                    if (key == null)
                    {
                        throw new InvalidOperationException(string.Format(Locale.GetString("Vit.Web", "ControlParameterKeyIsNotAString"),
                            i,
                            inputControlParameters[i].ToString()));
                    }
                    if (controlParameters.ContainsKey(key))
                    {
                        throw new InvalidOperationException(string.Format(Locale.GetString("Vit.Web", "ControlParameterKeyOccursMoreThanOnce"),
                            i,
                            key));
                    }

                    controlParameters[key] = value;
                }

                this.parameters = controlParameters;
            }
        }

        public Dictionary<string, string> Parameters
        {
            get
            {
                if (parameters == null)
                    SetWidgetParameters();
                return parameters;
            }
        }
    }
}
